package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/list")
public class ListApiController {

    // 注入es模板
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;


    // 初始化es，创建index
    @GetMapping("inner/createIndex")
    public Result createIndex(){

        // 使用es模板先创建es
        restTemplate.createIndex(Goods.class);
        // 将实体类中的结构put到esindex中
        restTemplate.putMapping(Goods.class);

        return Result.ok();
    }

    /**
     * 上架商品
     * @param skuId
     * @return
     */
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 下架商品
     * @param skuId
     * @return
     */
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 更新商品incrHotScore
     * @param skuId
     * @return
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId){

        searchService.incrHotScore(skuId);

        return Result.ok();
    }


    //  商品检索控制器！ Json -->javaObject
    @PostMapping
    public Result search(@RequestBody SearchParam searchParam){
        SearchResponseVo search = this.searchService.search(searchParam);
        return Result.ok(search);
    }


    // 监听rabbitmq，使用上架功能
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}))
    public void upperGoodsToEs(Long skuId, Message message, Channel channel){
        try {
            if (skuId!= null){
                // 调用上架
                this.upperGoods(skuId);
            }
        } catch (Exception e) {
            // 如果发生异常，下面的确认操作还是会继续执行的，try..catch就是处理异常的，
            // 这里如果有异常了，我们可以进行将异常的数据存到数据库去操作继续改进
            e.printStackTrace();
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    // 监听rabbitmq，使用上架功能
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_LOWER}))
    public void lowerGoodsToEs(Long skuId, Message message, Channel channel){
        try {
            if (skuId!= null){
                // 调用上架
                this.lowerGoods(skuId);
            }
        } catch (Exception e) {
            // 如果发生异常，下面的确认操作还是会继续执行的，try..catch就是处理异常的，
            // 这里如果有异常了，我们可以进行将异常的数据存到数据库去操作继续改进
            e.printStackTrace();
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
